/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode a=head;
        ListNode b=head;
        ListNode pre=new ListNode(-1);
        pre.next=head;
        for (int i = 0; i <k ; i++) {
            if(b==null) return head;
            b=b.next;
        }
        ListNode newHead=reverseBetween(a,b);
        a.next=reverseKGroup(b,k);
        return newHead;

    }
    public ListNode reverseBetween(ListNode a,ListNode b){
     ListNode cur=a;
     ListNode p=null;
     while(cur!=b){
         cur=a.next;
         a.next=p;
         p=a;
         a=cur;
     }
     return p;
    }


}
